<?php

import('classes.handler.Handler');
class BANHANGHandler extends Handler {
	function BANHANGHandler()
	{
	}

	function CTBANHANG($args, &$request) {
		$xMode  = Request::getUserVar("Mode");
		if ($xMode !==null)
			$this->edit_createCTBANHANG($request->getBaseUrl(), $args);
		else
			$this->listCTBANHANG($request->getBaseUrl());

	}

	function CTBANHANG_In($args, &$request) {
		import('classes.acct.doiso');
		$idphieu  = Request::getUserVar("idphieu");
		$nam  = Request::getUserVar("nam");
		$noco  = Request::getUserVar("noco");
		$journal =& Request::getJournal();
		$journalid =$journal->getId();

		$query = "SELECT a.soct, a.ngct, a.hoten, a.diachi, a.lydo, a.TK , b.tkdu , b.sotienvn FROM CTKT" .$journalid. $nam . " a LEFT JOIN ctktdt".$journalid.$nam . " b ON (a.idphieu = b.idphieu) ";
		
		$query = $query . "WHERE a.idphieu = '". $idphieu ."' " ;
		$ctktDao =& DAORegistry::getDAO('CTKTDAO');
        $ctkts =& $ctktDao->getCTKT_ThuChi($query, $noco);
		$sotien = floatval($ctkts[0]["sotienvn"]);
		$bangchu = doiso($sotien);

		$templateMgr =& TemplateManager::getManager();
		$templateMgr->assign('soct', $ctkts[0]["soct"]);
		$templateMgr->assign('ngct', $ctkts[0]["ngct"]);
		$templateMgr->assign('hoten', $ctkts[0]["hoten"]);
		$templateMgr->assign('diachi', $ctkts[0]["diachi"]);
		$templateMgr->assign('lydo', $ctkts[0]["lydo"]);
		$templateMgr->assign('solgctg', $ctkts[0]["solgctg"]);
		if ($noco == "N")
		{
			$templateMgr->assign('tenphieu', "Phiếu Thu");
			$templateMgr->assign('tkno', $ctkts[0]['tk']);
			$templateMgr->assign('tkco', $ctkts[0]['tkdu']);
		}
		else
		{
			$templateMgr->assign('tenphieu', "Phiếu Chi");
			$templateMgr->assign('tkco', $ctkts[0]['tk']);
			$templateMgr->assign('tkno', $ctkts[0]['tkdu']);

		}



		//$templateMgr->assign_by_ref('ctkts', $ctkts);
		$templateMgr->assign('noco', $noco);
		$templateMgr->assign('sotien', $sotien);
		$templateMgr->assign('bangchu', $bangchu);
		$templateMgr->display('acctCN/ThuChi.tpl');
	}

	function listCTBANHANG($baseurl) {
		$templateMgr =& TemplateManager::getManager();
		$user =& Request::getUser();
		$userId = $user->getId();
		$xma_dvcs = $user->getPath();
		$journal =& Request::getJournal();
		$journalid =$journal->getId();

		
		$gcNam  = Request::getUserVar("dateExpire1Year");
		$xThang1  = Request::getUserVar("dateExpire1Month");
	
		if ($xThang1)
		{
			$gcNam  = Request::getUserVar("dateExpire1Year");
			$gcNam2  = Request::getUserVar("dateExpire1Year");
			if ($gcNam !=$gcNam2)
			{
				echo '<script type="text/javascript">alert("Chi xem trong khoang thoi gian 1 nam"); </script>';
				return;
			}

			$xNgay1  = Request::getUserVar("dateExpire1Day");
			$xThang1  = Request::getUserVar("dateExpire1Month");
			$xNgay2  = Request::getUserVar("dateExpire2Day");
			$xThang2  = Request::getUserVar("dateExpire2Month");
			$date1 = mktime(0, 0, 0, $xThang1, $xNgay1, $gcNam);
			$date2 = mktime(0, 0, 0, $xThang2, $xNgay2, $gcNam);
			$ngay1 = date ( 'Y-m-d', $date1 );
			$ngay2 = date ( 'Y-m-d', $date2 );
		}
		else
		{
			$ngay1  = Request::getUserVar("ngay1");
			$ngay2  = Request::getUserVar("ngay2");
			if ($ngay1)
			{
				$gcNam = substr($ngay1,0,4);
			} 
			else
			{
				$xThang  =date("m", time());//
				$xNgay2  =date("d", time());//
				$xNgay1 = $xNgay2  - 10;
				if ($xNgay1 <=0)
					$xNgay1 = 1;
				$date1 = mktime(0, 0, 0, $xThang, $xNgay1, $gcNam);
				$date2 = mktime(0, 0, 0, $xThang, $xNgay2, $gcNam);
				$ngay1 = date ( 'Y-m-d', $date1 );
				$ngay2 = date ( 'Y-m-d', $date2 );
			}
		}

		$templateMgr->assign('xNam', $gcNam);

		$templateMgr->assign('Date1', $ngay1);  
		$templateMgr->assign('Date2', $ngay2);


		$ctktDao =& DAORegistry::getDAO('CTKHODAO');

		$dict = NewDataDictionary($ctktDao->_dataSource);
		$tables = $dict->MetaTables('TABLES', false);
		$tablename = "ctkho".$journalid. $gcNam;
		if (!in_array($tablename,$tables)) 
		{
				$ctktDao ->CreateTable($gcNam, $journalid );

			//echo $oldurl;
			//echo '<script type="text/javascript">alert("Khong co du lieu!"); </script>';
			//$oldurl= Request::url(null, "index", "CN_BANHANGCNO"); 
			//Request::redirectUrl($oldurl);
		}
		
		$ChiTiet  = Request::getUserVar("ChiTiet");
		$templateMgr->assign('mact', $ChiTiet);

		$journal =& Request::getJournal();
		$journalid =$journal->getId();
		$templateMgr->assign('journalid', $journalid);

		$templateMgr->assign('ChiTiet', $ChiTiet);

		switch($ChiTiet)
		{
			case "HDA":
				$templateMgr->assign('loaiphieu', "Hóa đơn bán hàng");
				$templateMgr->assign('mact', "HDA");
				break;
			case "HDD":
				$templateMgr->assign('loaiphieu', "Hóa đơn dịch vụ phải thu");
				$templateMgr->assign('mact', "HDD");
				break;

		}

		$templateMgr->assign('action', "Chung tu Thang ".$xThang."/".$gcNam);


       // lay so lieu
		// them phan Search

		$searchType = null;
		$searchMatch = null;
		$search = Request::getUserVar('search');
		$searchInitial = Request::getUserVar('searchInitial');
		if (!empty($search)) {
			$searchType = Request::getUserVar('searchField');
			$searchMatch = Request::getUserVar('searchMatch');

		} elseif (!empty($searchInitial)) {
			$searchInitial = String::strtoupper($searchInitial);
			$searchType = USER_FIELD_INITIAL;
			$search = $searchInitial;
		}

		$fieldOptions = Array(
		CTKT_FIELD_NGCT => 'Ngày chứng từ ',
		CTKT_FIELD_SOCT => 'S&#7889; ch&#7913;ng t&#7915; ',
		CTKT_FIELD_MAKHOX => 'Kho xuất ',
		CTKT_FIELD_MADVNO => 'Xuất cho đơn vị ',
		CTKT_FIELD_MAVT => 'Vật tư ',
		CTKT_FIELD_SL => 'Số lượng',
		CTKT_FIELD_DGB => 'Đơn giá bán',
		CTKT_FIELD_SOTIENVNB => 'Số tiền'
			);
		$templateMgr->assign('fieldOptions', $fieldOptions);
		$templateMgr->assign('searchField', $searchType);
		$templateMgr->assign('searchMatch', $searchMatch);
		$templateMgr->assign('search', $search);
		$templateMgr->assign('searchInitial', Request::getUserVar('searchInitial'));



		///
		$searchTypeMap = array(
		CTKT_FIELD_NGCT => 'A.NGCT',
		CTKT_FIELD_SOCT => 'A.SOCT',
		CTKT_FIELD_MAKHOX => 'A.MAKHOX',
		CTKT_FIELD_MADVNO => 'A.MADVNO',
		CTKT_FIELD_MAVT => 'B.MAVT',
		CTKT_FIELD_SL => 'B.SL',
		CTKT_FIELD_DGB => 'B.DGB',
		CTKT_FIELD_SOTIENVNB => 'B.SOTIENVNB'
		);
		$paramArray = array();
		$searchSql = '';

		if (!empty($search) && isset($searchTypeMap[$searchType])) {
			$fieldName = $searchTypeMap[$searchType];
			switch ($searchMatch) {
				case 'is':
					$searchSql = "AND LOWER($fieldName) = LOWER(?)";
					$paramArray[] = $search;
					break;
				case 'contains':
					$searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
					$paramArray[] = '%' . $search . '%';
					break;
				case 'startsWith':
					$searchSql = "AND LOWER($fieldName) LIKE LOWER(?)";
					$paramArray[] = $search . '%';
					break;
			}
		}

			$query = "SELECT a.idphieu, a.soct, a.ngct, a.makhox, a.madvno , b.mavt , b.sl , b.dgb, b.sotienvnb, b.id  FROM CTKHO".$journalid. $gcNam . " a LEFT JOIN ctkhodt".$journalid. $gcNam . " b ON (a.idphieu = b.idphieu) ";
			
			$query = $query . "WHERE DATE( a.ngct ) >= '" . $ngay1 . "' && DATE( a.ngct ) <=  '" . $ngay2 . "' && a.mact = '" . $ChiTiet . "'" ; 
			$query = $query . $searchSql;
			$query = $query . " ORDER BY ngct, soct";

			$rangeInfo = Handler::getRangeInfo('ctkts');
		
		$ctkts =& $ctktDao->getCTKHO_view($query, $paramArray, $rangeInfo);
		$templateMgr->assign_by_ref('ctkts', $ctkts);

		//url return from list
		$newurlRet = Request::url(null, 'index', 'CN_BANHANGCNO', null, array('ChiTiet' => $ChiTiet, 'Nam' => $gcNam));
		$templateMgr->assign('urlRet', $newurlRet );

		// url Sửa
		$newurlEdit = Request::url(null, null, 'CTBANHANG', null, array( 'Mode'=>'edit', 'ChiTiet' => 'HDA', 'Nam' => $gcNam ));
		$templateMgr->assign('urlEdit', $newurlEdit );


		//$newurl = Request::url(null, 'acctCN', 'CTBANHANG_In', null);
		//$templateMgr->assign('urlIn', $newurl );


		// url ban đầu của list (dùng sau khi xóa)
		$newurl = Request::url(null, null, 'CTBANHANG', null, array( 'ChiTiet' => $ChiTiet, 'Nam' => $gcNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
		$templateMgr->assign('url', $newurl );

		// url xóa
		$newurlDel = Request::url(null, 'acctCN', 'Delete_CTKHOList', null);
		$templateMgr->assign('urlDel', $newurlDel);


		$CN_SecDetail2 ="Xem";
		$templateMgr->assign('CN_SecDetail2', $CN_SecDetail2);
		$templateMgr->assign('CN_SecDetail', "CN_BANHANG");
		//$templateMgr->assign('searchSql', $searchSql);

		$templateMgr->addStyleSheet($baseurl.'/styles/dm.css');
		$templateMgr->addJavaScript("classes/acct/js/ShowHintMATK.js");

		$templateMgr->display('acctCN/CTBANHANGList.tpl');
	}


	function createDMTK() {
		$this->editDMTK();
	}

//	function RFR_DMDV(){
//		
//		$journal =& Request::getJournal();
//		$journalid =$journal->getId();;
//
//		$dmdvdao =& DAORegistry::getDAO('DMDVDAO');
//		$dmdv_initial = Request::getUserVar("dmdv_initial");;
//		$dmdvString = $dmdvdao->getSetting($journalid, $dmdv_initial);
//		echo $dmdvString ;
//	}

	
	function Ins_CTKHO(){
		$addmode  = Request::getUserVar("addmode");
		$journalid = Request::getUserVar('idJ') ;
		$Year = Request::getUserVar('year') ;
		$soct = Request::getUserVar("soct");
		$sosr = Request::getUserVar("sosr");
		$ngct = Request::getUserVar("ngct");
		$diengiai = Request::getUserVar("diengiai");
		$makhox = Request::getUserVar("makhox");
		$tk_dv = Request::getUserVar("tk_dv");
		$tk_thue = Request::getUserVar("tk_thue");
		$mact = Request::getUserVar("mact");
		$idphieu = Request::getUserVar("idphieu");
		


		$madvno = Request::getUserVar("madvno");
		$tendvno = Request::getUserVar("tendvno");
		$diachino = Request::getUserVar("diachino");
		$msthueno = Request::getUserVar("msthueno");


		$tableArr = Request::getUserVar("tableArr");
		$cautruc = Request::getUserVar("cautruc");

		$codulieu = 0;
		$succ = 1;
		$mysql = array();
		$i = 0;

		foreach ($tableArr as $v) {
			$dulieu = ""; 
			$mysql[$i] = "" ;

			foreach ($v as $key=>$value) 
			{
				$dulieu .= "$value";

				$fdulieu = "$value";
				if ($fdulieu !== "")
				{		$mysql[$i] .= ", ". $cautruc[$key]."  = '".$value."'";
				}
			}
			$mysql[$i] = str_replace("so_","",$mysql[$i]);
			if ($dulieu !== "")
			{
				$i++;
			}

		}

		if ($i==0)
		{
			echo "b.Không có dữ liệu";
			return ;
		}

		if ($addmode == "add"){
			$mysql_pa = "INSERT INTO CTKHO".$journalid. $Year. "  SET mact = 'HDA', ";
		}
		else{
			$mysql_pa = "UPDATE CTKHO".$journalid. $Year. "  SET ";
		}

		$mysql_pa .= "  soct='". $soct. "', ";
		$mysql_pa .= "  ngct='". $ngct. "', ";
		$mysql_pa .= "  sosr='". $sosr. "', ";
		$mysql_pa .= "  diengiai='". $diengiai. "', ";
		$mysql_pa .= "  madvno='". $madvno. "', ";
		$mysql_pa .= "  msthueno='". $msthueno. "', ";
		$mysql_pa .= "  diachino='". $diachino. "', ";
		$mysql_pa .= "  tendvno='". $tendvno. "', ";
		$mysql_pa .= "  makhox='". $makhox. "', ";
		$mysql_pa .= "  tk_dv='". $tk_dv. "', ";
		$mysql_pa .= "  tk_thue='". $tk_thue. "' ";


		if ($addmode == "edit")
			$mysql_pa .= " WHERE idphieu='". $idphieu . "'";

		$ctkthtDao =& DAORegistry::getDAO('CTKHODAO');

		$result =& $ctkthtDao->retrieve($mysql_pa);
		
		if ($addmode == "add") // them moi
		{
			$result =& $ctkthtDao->retrieve("SELECT MAX(idPhieu) as idPhieu FROM CTKHO".$journalid. $Year);
			$newidPhieus =$result->GetRowAssoc(false);
			$idphieu = $newidPhieus["idphieu"];

		}
		else
		{
			$mysqld = "DELETE FROM CTKHODT".$journalid. $Year. " WHERE idphieu = '". $idphieu."'";
			$result =& $ctkthtDao->retrieve($mysqld);
		}

		$mysql_pre = "INSERT INTO CTKHODT".$journalid. $Year. "  SET idphieu='". $idphieu . "'";

		for ($dong = 0; $dong < $i ; $dong++) {
			$mysqlQ = $mysql_pre.  $mysql[$dong]. ";";
			$result =& $ctkthtDao->retrieve($mysqlQ);
			if (!$result)
			{
				echo "a.Có lỗi dữ liệu!";
				return;
			}
		}
		echo $idphieu;  //.$mysql_pa

	}

	

	function Del_CTKHO(){
	}




	function edit_createCTBANHANG($baseurl, $args)
	{   

		$templateMgr =& TemplateManager::getManager();
		$xNam  = Request::getUserVar("dateExpire1Year");
		if (!$xNam)
			$xNam = Request::getUserVar("Nam");
		$templateMgr->assign('xNam', $xNam);

		$journal =& Request::getJournal();
		$journalid =$journal->getId();

		$templateMgr->assign('journalid', $journalid);

		$ChiTiet  = Request::getUserVar("ChiTiet");
		$templateMgr->assign('mact', $ChiTiet);


		//$this->validate();
		//$this->setupTemplate(true);
		$journal =& Request::getJournal();
		$journalid =$journal->getId();


		switch($ChiTiet)
		{
			case "HDA":
				import('classes.acct.form.HDBANHANGForm');
				$CTForm = new HDBANHANGForm(!isset($args) || empty($args) ? null : ( $args[0]));
				$templateMgr->assign('loaiphieu', "Hóa đơn Bán hàng");
				$templateMgr->assign('CTId', $this->HDBANHANGId);


				break;
		}

		

//		if ($CTForm->isLocaleResubmit()) {
//			$CTForm->readInputData();
//		} else {
//			// hien dang theo doi o day
//			$CTForm->initData();
//		}

		$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.min.js");
		$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.maskedinput.js");
		$templateMgr->addJavaScript("lib/pkp/js/lib/jquery/jquery.numberformatter-1.2.1.js");
		

		$dmntDao =& DAORegistry::getDAO('DMNTDAO');
		$dsmant =& $dmntDao->getDSMANT_MA();

		$dmkhoDao =& DAORegistry::getDAO('DMKHODAO');
		$dskhoa =& $dmkhoDao->getDSKHO();
		//$dskhoa = array_combine($dskhoa, $dskhoa);
		array_unshift($dskhoa, " ");
		$templateMgr->assign('makhoa', $dskhoa);

		$dmtkDao =& DAORegistry::getDAO('DMTKDAO');
		$matkAr = $dmtkDao->getDSMATK();
		$templateMgr->assign('matka', $matkAr);  //dùng để tra cứu

		$dmtkString = $dmtkDao->getSetting($journalid, 'tracuu');
		$templateMgr->assign('dmtks', $dmtkString);



		//$matkDVString = $dmtkDao->getSetting($journalid, 'tkdv');
		//$matkCPString = $dmtkDao->getSetting($journalid, 'tkcp');

		//$templateMgr->assign('dmtks', $dmtkString);
		//$templateMgr->assign('matks', $matkString);
		

		//$templateMgr->assign('tkdvs', $matkDVString);
		//$templateMgr->assign('tkcps', $matkCPString);


		
		$dmdvdao =& DAORegistry::getDAO('DMDVDAO');
		$dmdv_initial = "0";
		$templateMgr->assign('dmdv_initial',  $dmdv_initial);

		$madvAr = $dmdvdao->getSettingList($journalid, 'DMDVList');
		$templateMgr->assign('madvList',  $madvAr);     // dùng để tra cứu

		$madvString = $dmdvdao->getSettingMADV($journalid, 'madv');
		$madvAr = explode("---", $madvString);
		$madvAr = array_combine($madvAr, $madvAr);
		array_unshift($madvAr, "");
		$templateMgr->assign('madva',  $madvAr);
		

		$dmvtdao =& DAORegistry::getDAO('DMVTDAO');
		$dmvt_initial = "0";
		$templateMgr->assign('dmvt_initial',  $dmvt_initial);

//		$mavtAr = $dmvtdao->getListMAVT();
//		array_unshift($mavtAr, "");
//		$templateMgr->assign('mavta',  $mavtAr);

		$mavtAr = $dmvtdao->getSettingList($journalid, 'DMVTList');
		array_unshift($mavtAr, "");
		
		$templateMgr->assign('mavta',  $mavtAr);

		$templateMgr->assign('alphaList', explode(' ', __('common.InitList')));
		

		$xmant  = "VND";
		$templateMgr->assign('loaitien', $xmant);
		
		

		$templateMgr->addJavaScript('lib/pkp/js/lib/jquery/plugins/validate/jquery.validate.min.js');
		$templateMgr->addJavaScript('lib/pkp/js/jqueryValidatorI18n.js');



		/// 
		$xidPhieu  = Request::getUserVar("idPhieu");
		if ($xidPhieu == null)
		{
			$templateMgr->assign('action', "Th&#234;m ch&#7913;ng t&#7915; m&#7899;i");
			$newurl = Request::url(null, null, 'CTKHO', null, array('Mode' => 'Add', 'mant' => $xmant, 'ChiTiet' => $xChiTiet, 'Nam' => $xNam ));
			$templateMgr->assign('url', $newurl );
		}
		else
		{
			$templateMgr->assign('action', "S&#7917;a ch&#7913;ng t&#7915;");
			$newurl = Request::url(null, null, 'CTKHO', null, array('Mode' => 'Edit', 'mant' => $xmant, 'ChiTiet' => $xChiTiet, 'Nam' => $xNam ));
			$templateMgr->assign('url', $newurl );
		}
		


		$ctkhoDao =& DAORegistry::getDAO('CTKHODAO');
		$dict = NewDataDictionary($ctkhoDao->_dataSource);
		$tables = $dict->MetaTables('TABLES', false);
		$tablename = "ctkho".$journalid. $xNam;
		if (!in_array($tablename,$tables)) 
		{
			$ctkhoDao ->CreateTable($xNam, $journalid );

		}



		if ($xidPhieu  !== null)
		{
			$query = "SELECT a.idphieu, b.id, a.soct, sosr, a.ngct, a.diengiai, a.makhox, a.madvno, a.tendvno, a.msthueno, a.diachino, a.tk_dv, a.tk_thue, b.mavt, b.solo, b.handung, b.sl, b.dgb, b.sotienvnb, b.tlck, b.tk_ck, b.tk_dt FROM ctkho".$journalid.$xNam . " a LEFT JOIN ctkhodt".$journalid . $xNam . " b ON (a.idphieu = b.idphieu)  where a.idphieu = '". $xidPhieu. "' order by id";

			$ctkhos =& $ctkhoDao->getCTKHO_edit($query);
			$i=0;
			$actkho_mavt = array();
			$actkho_solo = array();
			$actkho_handung= array();
			$actkho_sl = array();
			$actkho_dgb = array();
			$actkho_sotienvnb = array();
			$actkho_tlck = array();
			$actkho_tk_ck = array();
			$actkho_tk_dt = array();

			$ngay1  = Request::getUserVar("ngay1");
			$ngay2  = Request::getUserVar("ngay2");

			foreach ($ctkhos as $key=>$row)
			{
				if ($i==0)
				{
					$soct = $row["soct"];
					$sosr = $row["sosr"];
					$ngct = $row["ngct"];
					$diengiai = $row["diengiai"];
					$makhox = $row["makhox"];
					$tmpArray = split('-', $makhox );
					$makhox = $tmpArray[0]; 
					$madvno = $row["madvno"];

					$tendvno = $row["tendvno"];
					$msthueno = $row["msthueno"];
					$diachino = $row["diachino"];
					$tk_dv = $row["tk_dv"];
						$tmpArray = split('-', $tk_dv );
						$tk_dv = $tmpArray[0]; 
					$tk_thue = $row["tk_thue"];
						$tmpArray = split('-', $tk_thue );
						$tk_thue = $tmpArray[0]; 


					$templateMgr->assign('soct', $soct);
					$templateMgr->assign('ngct', $ngct);
					$templateMgr->assign('sosr', $sosr);
					$templateMgr->assign('diengiai', $diengiai);
					$templateMgr->assign('makhox', $makhox);
					$templateMgr->assign('madvno', $madvno);
					$templateMgr->assign('tendvno', $tendvno);
					$templateMgr->assign('msthueno', $msthueno);
					$templateMgr->assign('diachino', $diachino);
					$templateMgr->assign('tk_dv', $tk_dv);
					$templateMgr->assign('tk_thue', $tk_thue);



				}
				$i++;
				$actkho_mavt[] = $row["mavt"];
				$actkho_solo[] = $row["solo"];
				$actkho_handung[] = $row["handung"];
				$actkho_sl[] = $row["sl"];
				$actkho_dgb[] = $row["dgb"];
				$actkho_sotienvnb[] = $row["sotienvnb"];
				$actkho_tlck[] = $row["tlck"];
				$actkho_tk_ck[] = $row["tk_ck"];
				$actkho_tk_dt[] = $row["tk_dt"];
			}


			$templateMgr->assign('sodong', $i);

			$sa = implode(",,,", $actkho_mavt);
			$templateMgr->assign('mavtss', $sa);
			$sa = implode(",,,", $actkho_solo);
			$templateMgr->assign('soloss', $sa);

			$sa = implode(",,,", $actkho_handung);
			$templateMgr->assign('handungss', $sa);
			$sa = implode(",,,", $actkho_sl);
			$templateMgr->assign('slss', $sa);

			$sa = implode(",,,", $actkho_dgb);
			$templateMgr->assign('dgbss', $sa);

			$sa = implode(",,,", $actkho_sotienvnb);
			$templateMgr->assign('sotienvnbss', $sa);

			$sa = implode(",,,", $actkho_tlck);
			$templateMgr->assign('tlckss', $sa);

			$sa = implode(",,,", $actkho_tk_ck);
			$templateMgr->assign('tk_ckss', $sa);


			$sa = implode(",,,", $actkho_tk_dt);
			$templateMgr->assign('tk_dtss', $sa);


			$templateMgr->assign('Mode', 'edit');
			$templateMgr->assign('idphieu', $xidPhieu);
			
			$CN_SecDetail2 ="Sửa chứng từ";

			//$newurlIns  = Request::url(null, null, 'Edit_ctkho', null, array('Nam' => $xNam, 'Mode'=> 'edit' ));

			$newurlIns  = Request::url(null, null, 'Ins_CTKHO', null, array('Nam' => $xNam, 'Mode'=> 'edit' ));

			$templateMgr->assign('urlIns', $newurlIns);

			$urlMain = Request::url(null, null, 'CN_ctkho', null, array('Nam' => $xNam, "ngay1"=> $ngay1, 'ngay2'=>$ngay2 ));
		}
		else
		{
			$result =& $ctkhoDao->retrieve("SELECT max(CONVERT(soct,UNSIGNED INTEGER)) AS num FROM CTKHO".$journalid. $xNam);
			$newsoct =$result->GetRowAssoc(false);
			$newsoct = $newsoct["num"]+1;
			$templateMgr->assign('soct', $newsoct);



			$newurlIns  = Request::url(null, null, 'Ins_CTKHO', null, array('Nam' => $xNam, 'Mode'=> 'add' ));
			$templateMgr->assign('urlIns', $newurlIns);

			
			$templateMgr->assign(urlMain , Request::url(null, 'acctCN', 'CN_MUAHANGCNO', null, array('Nam' => $xNam, 'Mode'=> 'add' )));

			//$urlMain = Request::url(null, null, 'CN_ctkho', null, array('Nam' => $xNam, 'Mode'=> 'add' ));

			$templateMgr->assign('sodong', 1);
			$templateMgr->assign('solo0', 'số lô');
			$templateMgr->assign('tk_ck0', '5212');
			$templateMgr->assign('tk_dt0', '51121');
			$templateMgr->assign('tk_dv', '131111');
			$templateMgr->assign('tk_thue', '33311');

			$dayh  = mktime(0, 0, 0, date("m")  , date("d"), date("Y")+3);	 
			$templateMgr->assign('handung0',date ('d/m/Y', $dayh));

			$CN_SecDetail2 ="Thêm mới chứng từ";
			$templateMgr->assign('Mode', 'add');
		}


		$urlS_getTenDMDV = Request::url(null, 'acctCN', 'getTen_DMDV');
		$urlS_getTenDMTK = Request::url(null, 'acctCN', 'getTen_DMTK');
		$urlS_getTenDMVT = Request::url(null, 'acctCN', 'getTen_DMVT');
		$urlS_getTenDMKHO = Request::url(null, 'acctCN', 'getTen_DMKHO');
		$urlS_getTenDMNT = Request::url(null, 'acctCN', 'getTen_DMNT');
		$templateMgr->assign('urlS_getTenDMDV', $urlS_getTenDMDV);
		$templateMgr->assign('urlS_getTenDMTK', $urlS_getTenDMTK);
		$templateMgr->assign('urlS_getTenDMVT', $urlS_getTenDMVT);
		$templateMgr->assign('urlS_getTenDMKHO', $urlS_getTenDMKHO);
		$templateMgr->assign('urlS_getTenDMNT', $urlS_getTenDMNT);


		$uGetSOCT = Request::url(null, 'acctCN', 'getSOCTnew', null);
		$templateMgr->assign('uGetSOCT', $uGetSOCT );


		$newurlIn = Request::url(null, 'acctCN', 'Ins_CTKHO', null);
		$templateMgr->assign('urlIn', $newurlIn );

		$newurlDel  = Request::url(null, null, 'Delete_CTKHO', null, array('Nam' => $xNam));
			$templateMgr->assign('urlDel', $newurlDel);



		$templateMgr->assign('idphieu', $xidPhieu);

		$urlS_DMDV = Request::url(null, 'acctCN', 'RFR_DMDV');
		$templateMgr->assign('urlS_DMDV', $urlS_DMDV);

		//$templateMgr->assign('urlMain', $urlMain);
		//echo $urlMain;
		// thêm ngày cho list
				$xThang  =date("m", time());//
				$xNgay2  =date("d", time());//

				$xNgay1 = $xNgay2  - 10;
				if ($xNgay1 <=0)
					$xNgay1 = 1;
				$date1 = mktime(0, 0, 0, $xThang, $xNgay1, $xNam);
				$date2 = mktime(0, 0, 0, $xThang, $xNgay2, $xNam);
				$ngay1 = date ( 'Y-m-d', $date1 );
				$ngay2 = date ( 'Y-m-d', $date2 );
		$templateMgr->assign('ngay1', $ngay1);
		$templateMgr->assign('ngay2', $ngay2);

		//

		$templateMgr->assign('CN_SecDetail', "CN_BANHANGCNO");
		$templateMgr->assign('CN_SecDetail2', $CN_SecDetail2);
		$CTForm->display();

	}

	function getSOCTnew(){
		$journalid = Request::getUserVar('idJ') ;
		$Year = Request::getUserVar('year') ;
		$mysql = "SELECT MAX(CAST(soct AS UNSIGNED)) as num FROM ctkho".$journalid. $Year;

		$ctkthtDao =& DAORegistry::getDAO('CTKHODAO');
		$result =& $ctkthtDao->retrieve($mysql);

		$newsoct =$result->GetRowAssoc(false);
		$newsoct = $newsoct["num"]+1;
		echo $newsoct ;


	}
	
	function Delete_CTKHOListj(){
		// xóa chứng từ từ List
		echo "1";
		return;
	}
	

	function Delete_CTKHOList(){

		$journalid = Request::getUserVar('journalid') ;
		$Year = Request::getUserVar('year') ;
		$idphieu = Request::getUserVar('idphieu') ;
		$url = Request::getUserVar('url') ;
		
		$ctkthtDao =& DAORegistry::getDAO('CTKHODAO');
		$mysqlQ = "DELETE FROM CTKHODT".$journalid. $Year. "  WHERE idPhieu='". $idphieu. "'";
		$result =& $ctkthtDao->retrieve($mysqlQ);
		if (gettype($result) == 'object')
		{
		}
		else
		{
			echo "2";
			return;
		}

		$mysqlQ = "DELETE FROM CTKHO".$journalid. $Year. "  WHERE idPhieu='". $idphieu. "'";
		$result =& $ctkthtDao->retrieve($mysqlQ);
		if (gettype($result) == 'object')
		{
			echo "1";
		}
		else
		{
			echo "2";
		}
	}

	function Delete_CTKHO(){
		$journalid = Request::getUserVar('journalid') ;
		$Year = Request::getUserVar('year') ;
		$idphieu = Request::getUserVar('idphieu') ;

		$ctkthtDao =& DAORegistry::getDAO('CTKHODAO');
		$mysqlQ = "DELETE FROM CTKHODT".$journalid. $Year. "  WHERE idPhieu='". $idphieu. "'";
		$result =& $ctkthtDao->retrieve($mysqlQ);
		if (gettype($result) == 'object')
		{
		}
		else
		{
			echo 2;
			return;
		}

		$mysqlQ = "DELETE FROM CTKHO".$journalid. $Year. "  WHERE idPhieu='". $idphieu. "'";
		$result =& $ctkthtDao->retrieve($mysqlQ);
		if (gettype($result) == 'object')
		{
			echo 1;
		}
		else
		{
			echo 2;
		}

	}
}

?>
